/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.swe622.test;

/**
 *
 * @author jasbh292
 */
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Server_Second extends Frame implements ActionListener, WindowListener {

//**************** single-threaded server, with UI

ServerSocket listenSocket;
Socket connection;

InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;

String message;

TextField hostDisplay, portDisplay;
TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button listenButton, quitButton;

public static final int DEFAULT_PORT = 8901;
String host;
int port;

  public Server_Second ( String s)  {

    super ( s );
    buildUI ();

  }  // end Server_Second constructor


// **************  Primary Listen & Respond Method


  public void connectClient ( )  {

    if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );
    else port = DEFAULT_PORT;

    try  {

      listenSocket = new ServerSocket ( port );
      logDisplay.setText ( "Server running on "+host+", port "+port+"\n" );

      connection = listenSocket.accept ();

      outStream = connection.getOutputStream ();
      outDataStream = new DataOutputStream ( outStream );
      inStream = connection.getInputStream ();
      inDataStream = new DataInputStream ( inStream );

      logDisplay.appendText ( "Connection request received\n" );
      logDisplay.appendText ( "Message, below, received\n" );

      try {
        message = inDataStream.readUTF ();
        msgDisplay.setForeground ( Color.red );
        msgDisplay.appendText ( message );
        }  // end try for input
      catch ( EOFException except ) {
        logDisplay.appendText ( "message EOF received\n" );
        }  // end catch
      catch ( IOException except ) {
        System.out.println ( "IO Exception raised" );
        except.printStackTrace ();
        }  // end catch

      System.out.println ("Getting ready to return message");
      message = msgDisplay.getText ();
      logDisplay.appendText ( "Message, below, returned to client \n" );
      outDataStream.writeUTF ( message );
      logDisplay.appendText ( "Connection closed\n" );
      connection.close ();
      return;

    }  // end try

    catch ( IOException except)  {
      except.printStackTrace ();
    }  // end catch

  }  // end connectClient

// **************

  public static void main ( String [ ] args )  {

    Server_Second server = new Server_Second ( "Server_Second" );

    server.addWindowListener ( server );

  }  // end main


  //***********  Utility Methods   ***********


// **************

  public void buildUI ()  {

  try {
    InetAddress here = InetAddress.getLocalHost ();
    host = here.getHostName ();
  }
  catch (UnknownHostException e) { ;}

  hostDisplay = new TextField ( host, 30 );
  hostDisplay.setEditable ( false );
  portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
  topPanel = new Panel ();
  topPanel.setLayout ( new GridLayout ( 2, 1 ) );
  topPanel.add ( hostDisplay );
  topPanel.add ( portDisplay );

  logDisplay = new TextArea ( 40, 10 );
  msgDisplay = new TextArea ( 40, 10 );
  middlePanel = new Panel ();
  middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
  middlePanel.add ( logDisplay );
  middlePanel.add ( msgDisplay );

  listenButton = new Button ( "Listen" );
  quitButton = new Button ( "Quit" );
  listenButton.addActionListener ( this );
  quitButton.addActionListener ( this );
  buttonPanel = new Panel ( );
  buttonPanel.add ( listenButton );
  buttonPanel.add ( quitButton );

  add ( "North", topPanel );
  add ( "Center", middlePanel );
  add ( "South", buttonPanel );

  resize ( 400, 450 );
  setVisible ( true );

  }  // end buildUI


  //***********  Interface Methods   ***********


  //****  ActionListener methods

  public void actionPerformed ( ActionEvent e )  {

    Object s = e.getSource();

    // *** process Button actions

    if ( s instanceof Button )  {

        if ( s == listenButton )  {
            connectClient ();
        }  // end listenButton

        if ( s == quitButton )  {
            hide ();
            dispose ();
            System.exit ( 0 );
            }  // end quitButton

    }  // end process Button actions

  }  // end actionPerformed



  //****  WindowListener methods

  public void windowActivated ( WindowEvent e )  {
  }

  public void windowDeactivated ( WindowEvent e )  {
  }

  public void windowOpened ( WindowEvent e )  {
  }

   public void windowClosed ( WindowEvent e )  {
  }

  public void windowClosing ( WindowEvent e )  {
    hide ();
    dispose ();
    System.exit(0);
  }

  public void windowIconified ( WindowEvent e )  {
  }

  public void windowDeiconified ( WindowEvent e )  {
  }


}  // end Server_Second

